<?php
class PNSpaOil extends PNObject {
  function PNSpaOil($init=null, $where='')
  {
    $this->PNObject();

    $this->_objType       = 'spaservice_spaoil';
    $this->_objField      = 'id';
    $this->_objPath       = 'form';    
    
    $this->_init($init, $where);
  }
  
  function getDataFromInputJSON()
  {
      $stdClass = json_decode(file_get_contents('php://input'));
      
      foreach($stdClass AS $key=>$val){
        $obj[$key] = $val;
      }
      if ($obj) {
          $this->_objData = $obj;
          $this->getDataFromInputPostProcess();
          return $this->_objData;
      }
      return $default;
   }
     
  function insertPostProcess()
  {
    $form = FormUtil::getPassedValue('form',FALSE);
    
    $spaCategory_spaOilTable = 'spaservice_spacategory_spaoil';
       
          
    foreach($form['spaCategory_id'] AS $value){
      $object[]  = array(
                'spaCategory_id' => $value,
                'spaOil_id' => $this->_objData['id']
          );
    }
    
    //Do the insert
    DBUtil::insertObjectArray($object, $spaCategory_spaOilTable);
  }
  
   function updatePostProcess()
  {
    
    $form = FormUtil::getPassedValue('form',FALSE);
    $category_spaoilTable = 'spaservice_spacategory_spaoil';
    
    $objID = $this->_objData[id];
    $result = NULL;
    if(isset($objID)){
      $pntables = pnDBGetTables();
      $tableSpaCategory_SpaOil = $pntables['spaservice_spacategory_spaoil'];
      $columnSpaCategory_SpaOil = $pntables['spaservice_spacategory_spaoil_column'];
    
      $sql = "SELECT
                $tableSpaCategory_SpaOil.$columnSpaCategory_SpaOil[id],
                $tableSpaCategory_SpaOil.$columnSpaCategory_SpaOil[spaCategory_id],
                $tableSpaCategory_SpaOil.$columnSpaCategory_SpaOil[spaOil_id]
              FROM
                $tableSpaCategory_SpaOil
              WHERE 
                $tableSpaCategory_SpaOil.$columnSpaCategory_SpaOil[spaOil_id] = $objID
              ORDER BY 
                $tableSpaCategory_SpaOil.$columnSpaCategory_SpaOil[id] ASC ";
      
      $column = array("id", "spaCategory_id","spaOil_id");
      $sqlResult = DBUtil::executeSQL($sql);
      $oldSpaCategory_SpaOil = DBUtil::marshallObjects ($sqlResult, $column);
    } 
    
    $oldSpaCategory_SpaOilItem = count($oldSpaCategory_SpaOil);
    foreach($form["spaCategory_id"] AS $value){
        if(is_array($value)){
          foreach($value AS $key2=>$value2){
            $updateObject[]  = array(
                    'id' => $key2,
                    'spaCategory_id' => $value2,
                    'spaOil_id' => $objID
            );
          }
          for($i=0;$i<=$oldSpaCategory_SpaOilItem;$i++){
            echo "oldSpaCategory_SpaOil[$i] : ".$oldSpaCategory_SpaOil[$i][id]." == ".$value[id]." : value";
            if($oldSpaCategory_SpaOil[$i]['id'] == $key2){
                unset($oldSpaCategory_SpaOil[$i]);
                echo " Deleted!";
            }
            echo " <HR>";
          }
        }else{
          $insertObject[]  = array(
                  'spaCategory_id' => $value,
                  'spaOil_id' => $objID
          );
        }
    }
    
    if(count($updateObject)>0){
       DBUtil::updateObjectArray($updateObject, $category_spaoilTable);
    }
    if(count($insertObject)>0){
       DBUtil::insertObjectArray($insertObject, $category_spaoilTable);
       
    }
    if(count($oldSpaCategory_SpaOil)>0){
      foreach($oldSpaCategory_SpaOil AS $value){
        $object[id] = $value[id];
        DBUtil::deleteObject($object,$category_spaoilTable);
        
      }
    }
   
  }
  
  function selectExtendResult(){
    $result = array();
    $id = $this->_objData['id'];
    
    
    if($id){
      $pntables = pnDBGetTables();
      $tableCategoryOil  = $pntables['spaservice_spacategory_spaoil'];
      $columnCategoryOil = $pntables['spaservice_spacategory_spaoil_column'];
      $tableCategory  = $pntables['spaservice_spacategory'];
      $columnCategory = $pntables['spaservice_spacategory_column'];
      
      $sql = "SELECT
                $tableCategoryOil.$columnCategoryOil[id],
                $tableCategoryOil.$columnCategoryOil[spaCategory_id],
                $tableCategoryOil.$columnCategoryOil[spaOil_id],
                $tableCategory.$columnCategory[title],
                $tableCategory.$columnCategory[titleTranslate]
              FROM
                $tableCategoryOil
              LEFT JOIN
                $tableCategory
              ON
                $tableCategoryOil.$columnCategoryOil[spaCategory_id] = $tableCategory.$columnCategory[id]
              WHERE 
                $tableCategoryOil.$columnCategoryOil[spaOil_id] = $id
              ORDER BY 
                $tableCategoryOil.$columnCategoryOil[id] ASC ";

      $column = array("id", "spaCategory_id","spaOil_id","title","titleTranslate");
      $sqlResult = DBUtil::executeSQL($sql);
      $result['spaCategoryOil'] = DBUtil::marshallObjects ($sqlResult, $column);
    }
   return $result;
  }
}